#include "ui_task.h"
#include "base_type.h"
#include "button_driver.h"
#include "Epaper_driver.h"
#include "paint.h"
#include "Epaper_param.h"
#include "Image/ImageData.h"

TaskHandle_t ui_task_handle;
static void ui_task(void *pvParameters);

esp_err_t ui_init(void)
{
    EPD_init();
    Paint_NewImage(ui_buffer, EPD_2IN9_V2_WIDTH, EPD_2IN9_V2_HEIGHT, 90, WHITE);  
    Paint_SelectImage(ui_buffer);
    Paint_Clear(WHITE);

    esp_err_t ret_err = ESP_OK;
    xTaskCreate((TaskFunction_t)ui_task,		// 任务函数
                        "ui_task",				    // 任务名称
                        2016,			                // 堆栈大小，实际大小为4 * usStackDepth
                        NULL,				            // 传递给任务函数的参数
                        2,					            // 任务优先级
                        &ui_task_handle 		// 任务句柄（可以理解为就是该任务本身）
                );
    if(ui_task_handle == NULL)
    {
        printf("screen_task create fail");
    }
    
    return ret_err;
}


static void ui_task(void *pvParameters)
{
    int pic_num = -1;
    const unsigned char* p_pic = NULL;

    /* 复位队列，防止初始化ui时用户点击按键 */
    xQueueReset(button_event_quene_handle);

    for(;;)
    {
        xQueueReceive(button_event_quene_handle, &pic_num, portMAX_DELAY);
        printf("task get pic %d\r\n", pic_num);

        switch (pic_num)
        {
        case 0:
            p_pic = gImage_snoopy;
            break;
        case 1:
            p_pic = gImage_dogLoveChicken;
            break;
        default:
            p_pic = gImage_snoopy;
            break;
        }

        Paint_Clear(WHITE);
        Paint_DrawBitMap(p_pic);
        EPD_2IN9_V2_Display(ui_buffer); 
    }
}